package yjh.controller;

import jakarta.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yjh.pojo.MistakesTopic;
import yjh.pojo.TopicTable;
import yjh.service.MistakesTopicService;
import yjh.service.TopicTableService;
import yjh.util.ResultData;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("topictable")
public class TopicTableController {
    @Autowired
    public TopicTableService topicTableService;
    @Autowired
    public MistakesTopicService mistakesTopicService;

    /**
     * 测试跨域连接
     * @return
     */
    @GetMapping("/test")
    public String test(@PathParam("userName") String userName){
        System.out.println(userName);

        if (userName!=null)
            return "{\"TestStatus\":\"OK!\"}";
        else
            return "{\"TestStatus\":\"Fail!\"}";

    }


    /**
     * 增加题目
     * @param topicTable
     * @return
     */
    @PostMapping("/add")
    public Map<String,Object> addTopic(@RequestBody TopicTable topicTable){
        if (topicTable==null) return ResultData.fail("input data is null");

        Integer tid = topicTable.getId();
        String topic = topicTable.getTopic();
        String answer = topicTable.getAnswer();
        Integer importanc = topicTable.getImportanc();

        if (topic.length() > 150 || answer.length() > 500
                || answer == null || topic == null
                || answer.isEmpty() || topic.isEmpty()
        ) return ResultData.fail("超出长度,请控制题目在150字以内、答案在500字以内");

        if (importanc > 4 || importanc <= 0) return ResultData.fail("非法数据，请键入1~4的数据");


        String addMessage;

        addMessage = topicTableService.addTopic(tid, topic, answer, importanc);


        if (addMessage != null) return ResultData.fail(addMessage);


        return ResultData.ok();
    }

    /**
     * 删除题目
     * @param topic
     * @return
     */
    @DeleteMapping("/delete/{topic}")
    public Map<String,Object> deleteTopic(@PathVariable String topic){

        if (topic == null)
            return ResultData.fail("input data is null");

        String deleteMessage = topicTableService.deleteTopic(topic);
        if (deleteMessage!=null) return ResultData.fail(deleteMessage);

        return ResultData.ok();
    }


    /**
     * 修改更新题目
     * @param topicTable
     * @param beforeChangeTopic
     * @return
     */
    @PutMapping("/update/{beforeChangeTopic}")
    public Map<String,Object> updateTopic(@RequestBody TopicTable topicTable,
                                          @PathVariable String beforeChangeTopic){
        if (topicTable == null || beforeChangeTopic==null||beforeChangeTopic.isEmpty())
            return ResultData.fail("input data is null");
        

        String afterChangeTopic = topicTable.getTopic();
        String answer = topicTable.getAnswer();
        Integer importanc = topicTable.getImportanc();

        //判断输入合法性（是否是合法长度）
        if (afterChangeTopic!=null&&afterChangeTopic.length()>150) return ResultData.fail("超出长度");
        if (answer!=null&&answer.length()>500) return ResultData.fail("超出长度");
        if (importanc!=null&& (importanc<=0||importanc>4) ) return ResultData.fail("长度不合法");

        String updateMessage = topicTableService.updateTopic(afterChangeTopic, answer, importanc, beforeChangeTopic);

        if (updateMessage!=null) return ResultData.fail(updateMessage);

        return ResultData.ok();

    }

    /**
     * 查询全部录入的题目、答案、重要度
     * @return
     */
    @GetMapping("/selectAll")
    public Map<String,Object> selectAllTopic(){
        List<TopicTable> selectTopicTables = topicTableService.findAllTopic();

        if (selectTopicTables.size()==0||selectTopicTables==null)
            return ResultData.fail("数据不存在");

        return ResultData.ok(selectTopicTables);
    }

    /**
     * 根据题目查询信息
     * @param topic
     * @return
     */
    @GetMapping("/selectOneByTopic/{topic}")
    public Map<String,Object> selectOneByTopic(@PathVariable String topic){
        if (topic==null||topic.isEmpty()) return ResultData.fail("数据有误");

        TopicTable oneByTopic = topicTableService.findOneByTopic(topic);

        if (oneByTopic == null) {
            return ResultData.fail("题目不存在");
        }

        return ResultData.ok(oneByTopic);
    }

    /**
     * 抽题
     * @param st
     * @param data
     * @return
     */
    @GetMapping("/selectOneTopicByRandom")
    public Map<String,Object> selectOneTopicByRandom(@PathParam("st") Integer st,
                                                     @PathParam("data") String data){

        System.out.println(st);
        System.out.println(data);


        //如果selectType==1，就全题库抽题
        //如果selectType==2，就按照重要度抽题
        //其他暂时按照非法输入进行处理
        TopicTable oneTopicByRandom;
        if (st==1 ||(st==2&&data!=null)) {
            oneTopicByRandom = topicTableService.findOneTopicByRandom(st,data);
            if (oneTopicByRandom == null) return ResultData.fail("查找失败");
        }else return ResultData.fail("非法输入");

        return ResultData.ok(oneTopicByRandom);
    }

    /**
     * 根据重要度查询相关题目
     * @param importanc
     * @return
     */
    @GetMapping("/selectAllByImportanc")
    public Map<String,Object> selectAllByImportanc(@PathParam("importanc") int importanc){
        if (!(importanc>0&&importanc<5)) return ResultData.fail("数据有误，请输入1~4整数");

        List<TopicTable> allByImportanc = topicTableService.findAllByImportanc(importanc);
        if (allByImportanc == null||allByImportanc.isEmpty()) {
            return ResultData.fail("数据不存在");
        }

        return ResultData.ok(allByImportanc);
    }
}
